﻿<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>鸿蒙应用权限控制与位置服务（Location Kit）</title>
  <link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>

<body class="stackedit">
  <div class="stackedit__html"><p>11_11日学习笔记</p>
<h1><a id="_4"></a>一、应用权限管控</h1>
<p><a href="https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-permission-mgmt-overview-V5#%E7%AE%80%E4%BB%8B">概述：</a></p>
<p>Ability Kit提供了一种允许应用访问<code>系统资源</code>（如：通讯录等）和<code>系统能力</code>（如：访问摄像头、麦克风等）的通用权限访问方式，来保护系统数据（包括用户个人数据）或功能，避免它们被不当或恶意使用。</p>
<p>快影app后台播放！</p>
<p>鸿蒙系统为每个应用提供一个tokenid（令牌），作为 应用的唯一标识。</p>
<p>应用权限保护的对象可以分为数据和功能：</p>
<p>● <code>数据</code> 包括个人数据（如照片、通讯录、日历、位置等）、设备数据（如设备标识、相机、麦克风等）。</p>
<p>● <code>功能</code> 包括设备功能（如访问摄像头/麦克风、打电话、联网等）、应用功能（如弹出悬浮窗、创建快捷方式等）。</p>
<blockquote>
<p>系统采用**<code>TokenID</code>**作为应用的唯一标识。权限管理服务通过应用的TokenID来管理应用的AT（Access Token）信息，包括应用身份标识APP ID、子用户ID、应用分身索引信息、应用APL、应用权限授权状态等。在资源使用时，系统将通过TokenID作为唯一身份标识映射获取对应应用的权限授权状态信息，并依此进行鉴权，从而管控应用的资源访问行为。</p>
<p>系统支持多用户特性和应用分身特性，同一个应用在不同的子用户下和不同的应用分身下会有各自的AT，这些AT的TokenID也是不同的。</p>
</blockquote>
<h2><a id="_24"></a>授权方式分类：</h2>
<h3><a id="1system_granthttpsdeveloperhuaweicomconsumercndocharmonyosguidesV5permissionsforallV5system_grant_26"></a>1、<a href="https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/permissions-for-all-V5#system_grant%E7%B3%BB%E7%BB%9F%E6%8E%88%E6%9D%83%E6%9D%83%E9%99%90%E5%88%97%E8%A1%A8">system_grant（系统授权）</a></h3>
<p><code>（module.json5）申请权限</code></p>
<p>应用被允许访问的数据<strong>不会涉及到用户或设备的敏感信息</strong>。</p>
<p>（如果在应用中申请了system_grant权限，那么系统会在用户安装应用时，自动把相应权限授予给应用。）</p>
<h3><a id="2user_granthttpsdeveloperhuaweicomconsumercndocharmonyosguidesV5permissionsforallV5user_grant_34"></a>2、<a href="https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/permissions-for-all-V5#user_grant%E7%94%A8%E6%88%B7%E6%8E%88%E6%9D%83%E6%9D%83%E9%99%90%E5%88%97%E8%A1%A8">user_grant（用户授权）</a></h3>
<p><code>（module.json5）申请权限 + 运行时弹窗请求授权</code></p>
<p><strong>涉及用户敏感信息</strong></p>
<p>该类型权限不仅需要①在安装包中申请权限，还需要在应用动态运行时，②通过发送弹窗的方式请求用户授权。</p>
<p>面试题：请问那些是用户授权的权限？那些是系统授权的权限？</p>
<p>系统授权权限：</p>

<table>
<thead>
<tr>
<th>ohos.permission.INTERNET</th>
<th>允许使用网络资源</th>
</tr>
</thead>
<tbody>
<tr>
<td>ohos.permission.KEEP_BACKGROUND_RUNNING</td>
<td>允许Ability在后台持续运行</td>
</tr>
<tr>
<td>ohos.permission.USE_BLUETOOTH</td>
<td>允许应用查看蓝牙的配置</td>
</tr>
<tr>
<td>ohos.permission.PRINT</td>
<td>允许应用获取打印框架的能力</td>
</tr>
<tr>
<td>ohos.permission.ACCELEROMETER</td>
<td>允许应用读取加速度传感器的数据</td>
</tr>
<tr>
<td>ohos.permission.VIBRATE</td>
<td>允许应用控制马达振动</td>
</tr>
</tbody>
</table><p>用户授权的权限：</p>

<table>
<thead>
<tr>
<th>ohos.permission.APP_TRACKING_CONSENT</th>
<th>允许应用读取开放匿名设备标识符</th>
</tr>
</thead>
<tbody>
<tr>
<td>ohos.permission.CAMERA</td>
<td>允许应用使用相机</td>
</tr>
<tr>
<td>ohos.permission.DISTRIBUTED_DATASYNC</td>
<td>允许不同设备间的数据交换</td>
</tr>
<tr>
<td>ohos.permission.APPROXIMATELY_LOCATION</td>
<td>允许应用获取设备模糊位置信息</td>
</tr>
<tr>
<td>ohos.permission.LOCATION</td>
<td>允许应用获取设备位置信息</td>
</tr>
<tr>
<td>ohos.permission.WRITE_MEDIA</td>
<td>允许应用读写用户外部存储中的媒体文件信息</td>
</tr>
</tbody>
</table><h2><a id="_66"></a>配置过程：</h2>
<h3><a id="_68"></a>系统授权配置</h3>
<p>配置文件授权：</p>
<p>1、首先需要在项目的配置文件（module.json5）中，申请系统授予权限：</p>
<p><img src="https://i-blog.csdnimg.cn/direct/de9cb780026d415a89bc07e32d59e5be.png" alt="请添加图片描述"><br>
<img src="https://i-blog.csdnimg.cn/direct/f8725b7b51a44e38b287db240228d92f.png" alt="请添加图片描述"><br>
<img src="https://i-blog.csdnimg.cn/direct/615a9f4cb7ef4257b4ceec3ebb301042.png" alt="请添加图片描述"></p>
<pre><code class="prism language-typescript"><span class="token comment">//当前应用模块向用户申请各种权限（系统授予+用户授予）</span>
    <span class="token string">"requestPermissions"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token comment">//      ①向系统申请  ‘系统授予权限’</span>
      <span class="token punctuation">{</span>
        <span class="token string">"name"</span><span class="token operator">:</span> <span class="token string">"ohos.permission.INTERNET"</span> <span class="token punctuation">,</span>   <span class="token comment">//互联网访问权限（系统权限）</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span>
        <span class="token string">"name"</span><span class="token operator">:</span> <span class="token string">"ohos.permission.KEEP_BACKGROUND_RUNNING"</span> <span class="token comment">//后台运行（系统权限），应用在后台可以继续运行</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
</code></pre>
<p>（前应用模块向用户申请各种权限（系统授予+用户授予））</p>
<h3><a id="user_grant_95"></a>当应用申请user_grant权限时，需要完成以下步骤：</h3>
<p>① 在配置文件（module.json5）中，声明应用需要请求的权限。<br>
<img src="https://i-blog.csdnimg.cn/direct/befced6ba791464888d3b7da3930fba7.png" alt="请添加图片描述"><br>
<img src="https://i-blog.csdnimg.cn/direct/0fab3dfea45140708c25d8523dc97bf4.png" alt="请添加图片描述"><br>
<img src="https://i-blog.csdnimg.cn/direct/76756e1a09044cda92e10313073e6d38.png#pic_center" alt="在这里插入图片描述"><br>
<img src="https://i-blog.csdnimg.cn/direct/d8f24c5d3f6e44ef96dea1d54337da64.png" alt="在这里插入图片描述"><br>
<img src="https://i-blog.csdnimg.cn/direct/73505535891d4a28b744935f83f51f16.png" alt="在这里插入图片描述"><br>
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/4e47317452464e08814e2678734cdfd4.png#pic_center</p>
<pre><code class="prism language-typescript"><span class="token string">"requestPermissions"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
  <span class="token comment">//②向用户申请  “用户授予权限”</span>
  <span class="token punctuation">{</span>
    <span class="token string">"name"</span><span class="token operator">:</span> <span class="token string">"ohos.permission.APPROXIMATELY_LOCATION"</span><span class="token punctuation">,</span>		 <span class="token comment">//模糊定位</span>
    <span class="token string">"reason"</span><span class="token operator">:</span> <span class="token string">'$string:Location_Reason'</span><span class="token punctuation">,</span>		<span class="token comment">//申请当前权限的原因说明</span>
    <span class="token string">"usedScene"</span><span class="token operator">:</span> <span class="token punctuation">{</span>												<span class="token comment">//申请的权限具体使用场景说明</span>
      <span class="token string">"when"</span><span class="token operator">:</span> <span class="token string">"always"</span><span class="token punctuation">,</span>										<span class="token comment">//何时使用该权限！    inuse在使用中，always总是</span>
      <span class="token string">"abilities"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"EntryAbility"</span><span class="token punctuation">]</span>					<span class="token comment">//哪些组件需要使用该权限</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    <span class="token string">"name"</span><span class="token operator">:</span> <span class="token string">"ohos.permission.LOCATION"</span><span class="token punctuation">,</span> <span class="token comment">//精准定位</span>
    <span class="token string">"reason"</span><span class="token operator">:</span> <span class="token string">'$string:Location_Reason'</span><span class="token punctuation">,</span>
    <span class="token string">"usedScene"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
      <span class="token string">"abilities"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"EntryAbility"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token string">"when"</span><span class="token operator">:</span> <span class="token string">"always"</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
</code></pre>
<p>② 将应用中需要申请权限的目标对象与对应目标权限进行关联，让用户明确地知道，哪些操作需要用户向应用授予指定的权限。</p>
<p>③ 运行应用时，在用户触发访问操作目标对象时应该调用接口，精准触发动态授权弹框。该接口的内部会检查当前用户是否已经授权应用所需的权限，如果当前用户尚未授予应用所需的权限，该接口会拉起动态授权弹框，向用户请求授权。</p>
<p>④ 检查用户的授权结果，确认用户已授权才可以进行下一步操作。<br>
<img src="https://i-blog.csdnimg.cn/direct/273c3f05e979440aaa0f40800d05eb88.png#pic_center" alt="在这里插入图片描述"></p>
<blockquote>
<p>每次执行需要目标权限的操作时，应用都必须使用checkAccessToken()函数检查自己是否已经具有该权限每次访问受目标权限保护的接口之前，都需要使用requestPermissionsFromUser()接口请求相应的权限</p>
</blockquote>
<p>页面现实时，弹出 “ 定位授权对话框</p>
<p>创建onpageShow()方法</p>
<pre><code class="prism language-typescript"><span class="token comment">//页面现实时，弹出 “ 定位授权对话框 ”</span>
<span class="token function">onPageShow</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  
<span class="token punctuation">}</span>
</code></pre>
<p><img src="https://i-blog.csdnimg.cn/direct/f237a80c4bf24479ab62a4a57c9122fb.png" alt="请添加图片描述"></p>
<p>1、声明需要用户授权的权限列表<br>
<img src="https://i-blog.csdnimg.cn/direct/eb12234e5f0f4803ac6b7041ff57c8f3.png" alt="请添加图片描述"></p>
<p>2、检查之前是否对这些权限授权过<br>
<img src="https://i-blog.csdnimg.cn/direct/8f79d3222f85427bb89c32d9bb2f7b7f.png" alt="请添加图片描述"></p>
<p>3、如果之前从未授权过，则弹出“授权请求对话框”<br>
<img src="https://i-blog.csdnimg.cn/direct/6dbceff0bb75486a978f3564892be7e7.png" alt="请添加图片描述"><br>
完整代码：</p>
<p><img src="https://i-blog.csdnimg.cn/direct/e16f11e0f5ff4a528eee308ac693065e.png" alt="请添加图片描述"></p>
<pre><code class="prism language-typescript"><span class="token keyword">import</span> <span class="token punctuation">{</span> Permissions <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@ohos.privacyManager'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> abilityAccessCtrl<span class="token punctuation">,</span> bundleManager <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@kit.AbilityKit'</span>

@Entry
@Component
struct Index <span class="token punctuation">{</span>

  <span class="token comment">//页面现实时，弹出 “ 定位授权对话框 ”</span>
  <span class="token keyword">async</span> <span class="token function">onPageShow</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 1、声明需要用户授权的权限列表</span>
    <span class="token keyword">let</span> list<span class="token operator">:</span>Permissions<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
      <span class="token string">"ohos.permission.APPROXIMATELY_LOCATION"</span><span class="token punctuation">,</span> <span class="token comment">//模糊定位</span>
      <span class="token string">"ohos.permission.LOCATION"</span>                <span class="token comment">//精确定位 ---》用精确定位，必须要有模糊定位为前提！</span>
    <span class="token punctuation">]</span>
    <span class="token comment">// 2、检查之前是否对这些权限进行过授权   TokenID =&gt; AccessManager =&gt; AccessToken =&gt; 过往的授权记录</span>
    <span class="token keyword">let</span> bundleInfo <span class="token operator">=</span> bundleManager<span class="token punctuation">.</span><span class="token function">getBundleInfoForSelfSync</span><span class="token punctuation">(</span>bundleManager<span class="token punctuation">.</span>BundleFlag<span class="token punctuation">.</span><span class="token constant">GET_BUNDLE_INFO_WITH_APPLICATION</span><span class="token punctuation">)</span>    <span class="token comment">//得到资源报信息</span>
    <span class="token keyword">let</span> tokenID <span class="token operator">=</span> bundleInfo<span class="token punctuation">.</span>appInfo<span class="token punctuation">.</span>accessTokenId <span class="token comment">//从资源包信息中获取  权限令牌编号</span>

    <span class="token keyword">let</span> acsMgr <span class="token operator">=</span> abilityAccessCtrl<span class="token punctuation">.</span><span class="token function">createAtManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span>    <span class="token comment">//AT表示AccessToken</span>
    <span class="token keyword">let</span> grantStatus0 <span class="token operator">=</span> acsMgr<span class="token punctuation">.</span><span class="token function">checkAccessTokenSync</span><span class="token punctuation">(</span>tokenID<span class="token punctuation">,</span>list<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>   <span class="token comment">//检查访问令牌,返回值是授权状态</span>
    <span class="token keyword">let</span> grantStatus1 <span class="token operator">=</span> acsMgr<span class="token punctuation">.</span><span class="token function">checkAccessTokenSync</span><span class="token punctuation">(</span>tokenID<span class="token punctuation">,</span>list<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>   <span class="token comment">//检查访问令牌,返回值是授权状态</span>

    <span class="token comment">// 3、如果之前从未授权过，则弹出请求对话框</span>
    <span class="token comment">// console.log('--授权状态:',grantStatus0)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>grantStatus0 <span class="token operator">===</span> abilityAccessCtrl<span class="token punctuation">.</span>GrantStatus<span class="token punctuation">.</span><span class="token constant">PERMISSION_DENIED</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>   <span class="token comment">//-1表示尚未授权过DENIED,0表示已经授权</span>
      <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token keyword">await</span> acsMgr<span class="token punctuation">.</span><span class="token function">requestPermissionsFromUser</span><span class="token punctuation">(</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>list<span class="token punctuation">)</span>    <span class="token comment">//list表示两个权限 --&gt; 权限列表 ---permissionList:permissions</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>result<span class="token punctuation">.</span>authResults<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>    <span class="token comment">//授权结果，authResults[]两个权限</span>
        <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'---定位相关的两个权限通过了用户授权'</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>    <span class="token comment">//-1表示DENIED</span>
        <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'---用户拒绝了定位相关的两个权限授权'</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

  <span class="token punctuation">}</span>

  <span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">Column</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">'首页'</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">fontSize</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span>

    <span class="token punctuation">}</span>
    <span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token string">'100%'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token string">'100%'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>运行结果：</p>
<p><img src="https://i-blog.csdnimg.cn/direct/5efbd0fd263f412895aad95b7881c8ec.png" alt="请添加图片描述"></p>
<h1><a id="Location_Kit_210"></a>二、定位服务（Location Kit）：</h1>
<h2><a id="_212"></a>一次性获取用户当前位置</h2>
<p>获取当前位置：</p>
<pre><code class="prism language-typescript"><span class="token keyword">let</span> location <span class="token operator">=</span> <span class="token keyword">await</span> geoLocationManager<span class="token punctuation">.</span><span class="token function">getCurrentLocation</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<p><img src="https://i-blog.csdnimg.cn/direct/16a35d5b88cc4dc9a286b07f05946465.png" alt="请添加图片描述"><br>
<img src="https://i-blog.csdnimg.cn/direct/f3184aad4f574f6f97195e6293a5df9f.png" alt="请添加图片描述"><br>
原因：没有把当前位置打开<br>
<img src="https://i-blog.csdnimg.cn/direct/3b6c8b9f6b2b4cc8a71058178c30f7c9.png" alt="请添加图片描述"><br>
<img src="https://i-blog.csdnimg.cn/direct/5fdbc583a6c547b9b4ed6df46547b8fd.png" alt="请添加图片描述"></p>
<h3><a id="_225"></a>在获取用户当前地理位置之后，进一步通过经纬度信息反编码出具体的地理位置信息。</h3>
<p><img src="https://i-blog.csdnimg.cn/direct/8b27016a2d4b44108197dfae1a35df45.png" alt="请添加图片描述"><br>
完整代码：<br>
<img src="https://i-blog.csdnimg.cn/direct/3e2b38c0e0454c90801e9ced7d97bac2.png" alt="请添加图片描述"><br>
完整代码：</p>
<pre><code class="prism language-typescript"><span class="token keyword">import</span> <span class="token punctuation">{</span> Permissions <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@ohos.privacyManager'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> abilityAccessCtrl<span class="token punctuation">,</span> bundleManager <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@kit.AbilityKit'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> geoLocationManager <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@kit.LocationKit'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> <span class="token constant">JSON</span> <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@kit.ArkTS'</span>

@Entry
@Component
struct Index <span class="token punctuation">{</span>

  <span class="token comment">//页面现实时，弹出 “ 定位授权对话框 ”</span>
  <span class="token keyword">async</span> <span class="token function">onPageShow</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 1、声明需要用户授权的权限列表</span>
    <span class="token keyword">let</span> list<span class="token operator">:</span>Permissions<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
      <span class="token string">"ohos.permission.APPROXIMATELY_LOCATION"</span><span class="token punctuation">,</span> <span class="token comment">//模糊定位</span>
      <span class="token string">"ohos.permission.LOCATION"</span>                <span class="token comment">//精确定位 ---》用精确定位，必须要有模糊定位为前提！</span>
    <span class="token punctuation">]</span>
    <span class="token comment">// 2、检查之前是否对这些权限进行过授权   TokenID =&gt; AccessManager =&gt; AccessToken =&gt; 过往的授权记录</span>
    <span class="token keyword">let</span> bundleInfo <span class="token operator">=</span> bundleManager<span class="token punctuation">.</span><span class="token function">getBundleInfoForSelfSync</span><span class="token punctuation">(</span>bundleManager<span class="token punctuation">.</span>BundleFlag<span class="token punctuation">.</span><span class="token constant">GET_BUNDLE_INFO_WITH_APPLICATION</span><span class="token punctuation">)</span>    <span class="token comment">//得到资源报信息</span>
    <span class="token keyword">let</span> tokenID <span class="token operator">=</span> bundleInfo<span class="token punctuation">.</span>appInfo<span class="token punctuation">.</span>accessTokenId <span class="token comment">//从资源包信息中获取  权限令牌编号</span>

    <span class="token keyword">let</span> acsMgr <span class="token operator">=</span> abilityAccessCtrl<span class="token punctuation">.</span><span class="token function">createAtManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span>    <span class="token comment">//AT表示AccessToken</span>
    <span class="token keyword">let</span> grantStatus0 <span class="token operator">=</span> acsMgr<span class="token punctuation">.</span><span class="token function">checkAccessTokenSync</span><span class="token punctuation">(</span>tokenID<span class="token punctuation">,</span>list<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>   <span class="token comment">//检查访问令牌,返回值是授权状态</span>
    <span class="token keyword">let</span> grantStatus1 <span class="token operator">=</span> acsMgr<span class="token punctuation">.</span><span class="token function">checkAccessTokenSync</span><span class="token punctuation">(</span>tokenID<span class="token punctuation">,</span>list<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>   <span class="token comment">//检查访问令牌,返回值是授权状态</span>

    <span class="token comment">// 3、如果之前从未授权过，则弹出请求对话框</span>
    <span class="token comment">// console.log('--授权状态:',grantStatus0)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>grantStatus0 <span class="token operator">===</span> abilityAccessCtrl<span class="token punctuation">.</span>GrantStatus<span class="token punctuation">.</span><span class="token constant">PERMISSION_DENIED</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>   <span class="token comment">//-1表示尚未授权过DENIED,0表示已经授权</span>
      <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token keyword">await</span> acsMgr<span class="token punctuation">.</span><span class="token function">requestPermissionsFromUser</span><span class="token punctuation">(</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>list<span class="token punctuation">)</span>    <span class="token comment">//list表示两个权限 --&gt; 权限列表 ---permissionList:permissions</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>result<span class="token punctuation">.</span>authResults<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>    <span class="token comment">//授权结果，authResults[]两个权限</span>
        <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'---定位相关的两个权限通过了用户授权'</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>    <span class="token comment">//-1表示DENIED</span>
        <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'---用户拒绝了定位相关的两个权限授权'</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

  <span class="token punctuation">}</span>

  <span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">Column</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">'首页'</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">fontSize</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span>

      <span class="token function">Button</span><span class="token punctuation">(</span><span class="token string">'1、一次性获取用户当前位置'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">onClick</span><span class="token punctuation">(</span><span class="token keyword">async</span> _<span class="token operator">=&gt;</span><span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
          <span class="token keyword">let</span> location <span class="token operator">=</span> <span class="token keyword">await</span> geoLocationManager<span class="token punctuation">.</span><span class="token function">getCurrentLocation</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
          <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'获取位置信息成功！'</span><span class="token punctuation">,</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>location<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'获取定位信息失败！：'</span><span class="token punctuation">,</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>

      <span class="token punctuation">}</span><span class="token punctuation">)</span>

      <span class="token function">Button</span><span class="token punctuation">(</span><span class="token string">'1-2、一次性获取用户当前位置'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">onClick</span><span class="token punctuation">(</span><span class="token keyword">async</span> _<span class="token operator">=&gt;</span><span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
          <span class="token keyword">let</span> loc <span class="token operator">=</span> <span class="token keyword">await</span> geoLocationManager<span class="token punctuation">.</span><span class="token function">getCurrentLocation</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
          <span class="token comment">// 根据经纬度信息反编码出地理信息 --- 需要在真机才有效</span>
          <span class="token keyword">let</span> request<span class="token operator">:</span>geoLocationManager<span class="token punctuation">.</span>ReverseGeoCodeRequest<span class="token operator">=</span> <span class="token punctuation">{</span>
            latitude<span class="token operator">:</span> loc<span class="token punctuation">.</span>latitude<span class="token punctuation">,</span>
            longitude<span class="token operator">:</span> loc<span class="token punctuation">.</span>longitude
          <span class="token punctuation">}</span>
          <span class="token keyword">let</span> list <span class="token operator">=</span> <span class="token keyword">await</span> geoLocationManager<span class="token punctuation">.</span><span class="token function">getAddressesFromLocation</span><span class="token punctuation">(</span>request<span class="token punctuation">)</span>

          <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'当前坐标所表示的地理位置'</span><span class="token punctuation">,</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'获取定位信息失败！'</span><span class="token punctuation">,</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span>

      <span class="token function">Button</span><span class="token punctuation">(</span><span class="token string">'2、持续获取用户位置的改变'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">onClick</span><span class="token punctuation">(</span>_<span class="token operator">=&gt;</span> <span class="token punctuation">{</span>

      <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token string">'100%'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token string">'100%'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2><a id="_310"></a>持续获取用户位置的改变</h2>
</div>
</body>

</html>
